$(document).ready(function(){
	firstGrapData();
	$("#submit").click(function(e){
		var heartword = $("#content").val();
		$.ajax({
			url : "/CLASS/liuyan.do",
			type : "post",
			datatype : "json",
			data :{
				"heartword" : heartword
			},
			success:function(data){
				var toData = 'data';
				var toObj = eval("(" + toData + ")");
				if(toObj.htp.heartword != null){
					$("#mood").prepend('<div class="col-sm-12 comments" style="margin-top:20px;"><div class="col-sm-12 bigComment" style="margin-top: 10px;border-bottom: 1px solid #ccc;"><img src="'+toObj.htp.photo+'" class="img"><div class="col-sm-8"><h6>'+ toObj.htp.membername +'</h6><p class="font">2014-35-99&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p class="say">'+toObj.htp.heartword+'</p></div><div class="col-sm-2 good"></div><a href="#" class="icon-trash bigtrash"></a></div><div class="col-sm-12" style="margin-top: 20px;"><div class="col-sm-1"></div><div class="col-sm-8 coments"><div class="col-sm-12" style="margin-bottom: 20px;"><input type="text" class="col-sm-10" style="height: 30px;"><a href="" class="reply col-sm-2">回复</a></div></div></div>').attr("disabled","true");
					$("#mood .good").append('<a href="javascript:void(0)" class="icon-thumbs-up">'+ toObj.htp.clicknum +'<input type="hidden" class="hidden" value="'+ toObj.htp.clicknum +'"></a><input type="hidden" class="hidden" value="'+ toObj.htp.id +'"><input type="hidden" class="hidden" value="'+ toObj.htp.userid +'">');
					alert(toObj.message);
				}else{
					return false;
				}
				setTimeout(function(){
					window.location.href="/CLASS/xqq.do";
				}, 100);
			},
			error:function() {
				alert("出错了");
			}
		})
		e.preventDefault();
	})
	$("#ul").delegate("li","click",function(){
		getData();
	})
	$("#mood").delegate(".icon-thumbs-up","click",function(e){
		getPraise();
		e.preventDefault();
	})
	$("#mood").delegate(".reply","click",function(){
		getComment();
	})
	$("#mood").delegate(".bigComment","mouseover",function(){
		$(this).find(".icon-trash").css("display","block");
	})
	$("#mood").delegate(".bigComment","mouseout",function(){
		$(this).find(".icon-trash").css("display","none");
	})
	$("#mood").delegate(".smallComment","mouseover",function(){
		$(this).find(".icon-trash").css("display","block");
	})
	$("#mood").delegate(".smallComment","mouseout",function(){
		$(this).find(".icon-trash").css("display","none");
	})
	$("#mood").delegate(".bigtrash","click",function(){
		delWord();
	})
	$("#mood").delegate(".smalltrash","click",function(){
		delComment();
	})
})
//获取json中的当前页数
function getData(){
	var page = $(event.target).parents("li").find(".pagebar").val();
	var nowPage = {"page" : page};
	$("#mood").empty();
	getWay("/CLASS/getwords.do",nowPage,operateData);
}
//当文档加载完成时先获取第一页的数据
function firstGrapData(){
	var firstPage = {"page" : 1};
	getWay("/CLASS/getwords.do",firstPage,operateData);
}
function getWay(address,Data,callback){
	$.ajax({
		url : address,
		type : "post",
		datatype : "json",
		data : Data,
		success : function(data){
			callback(data);
		},
		error : function(){
			alert("有错哟!");
		}
	})
}
//获取页数的回调函数
function operateData(data){ 
	console.log(data);
	var list = Math.ceil(data.rowCount/10);
	if($("#ul").find(".pagebar").val() == undefined){
		for(var j = 1;j <= list;j++){
			$("#ul").append('<li><a href="javascript:void(0)">'+ j +'</a><input type="hidden" value="'+ j +'" class="pagebar"></li>');
		}
	}
	for(var i in data.data){
		console.log(data.data[i]);
		$("#mood").append('<div class="col-sm-12 comments" style="margin-top:20px;"><div class="col-sm-12 bigComment" style="margin-top: 10px;border-bottom: 1px solid #ccc;"><img src="'+data.data[i].photo+'" class="img"><div class="col-sm-8"><h6>'+ data.data[i].membername +'</h6><p class="font">'+ data.data[i].createtime +'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p class="say">'+data.data[i].heartword+'</p></div><div class="col-sm-2 good"></div><a href="javascript:void(0)" class="icon-trash bigtrash"></a></div><div class="col-sm-12 coments" style="margin-top: 20px;"><div class="col-sm-1"></div><div class="col-sm-8"><div class="col-sm-12 coment"></div><div class="col-sm-12 answer" style="margin-bottom: 20px;"><input type="text" class="col-sm-10" style="height: 30px;"><a href="javascript:void(0)" class="reply col-sm-2">回复</a></div></div></div>');
		if(data.data[i].admirestatus == "0"){
			$("#mood .good:eq("+ i +")").append('<a href="javascript:void(0)" class="icon-thumbs-up" style="color : orange;">'+ data.data[i].clicknum +'<input type="hidden" class="hidden" value="'+ data.data[i].clicknum +'"><input type="hidden" class="hidden" value="'+ data.data[i].id +'"><input type="hidden" class="hidden" value="'+ data.data[i].userid +'"></a>');			
		}else if(data.data[i].admirestatus == "1"){
			$("#mood .good:eq("+ i +")").append('<a href="javascript:void(0)" class="icon-thumbs-up">'+ data.data[i].clicknum +'<input type="hidden" class="hidden" value="'+ data.data[i].clicknum +'"><input type="hidden" class="hidden" value="'+ data.data[i].id +'"><input type="hidden" class="hidden" value="'+ data.data[i].userid +'"></a>');						
		}
		
		
		$.each(data.data[i].list,function(j,n){
				if(!$.isEmptyObject(data.data[i].list)){
					$(".coment:eq("+ i +")").append('<div class="col-sm-12 smallComment"><img src="'+ n.photo +'" alt="" class="comment"><div class="col-sm-2"><h6>'+ n.membername +'</h6><p class="font">'+ n.createtime +'</p></div><div class="col-sm-8"><p>'+ n.comment +'</p></div><input type="hidden" class="hidden" value="'+ n.commentid +'"><input type="hidden" class="hidden" value="'+ n.userid +'"><a href="javascript:void(0)" class="icon-trash smalltrash"></a></div>');
			}
		})
	}
}
//点赞的功能
function getPraise(){
	var id = $(event.target).parents(".good").find(".hidden:eq(1)").val();
	var clicknum = $(event.target);
	$.ajax({
		url : "/CLASS/admire.do?heartwordid="+id,
		type : "post",
		datatype : "json",
		success : function(data){
			console.log(data);
			if(data.clicknum != "" ){
				clicknum.html(data.clicknum).css("color","orange");
			}else{
				return false;
			}
			alert(data.message);
		}
	})
}
//评论功能
function getComment(){
	var comment = $(event.target).parents(".answer").find("input").val();
	var comment_input = $(event.target).parent(".answer").find("input");
	var id = $(event.target).parents(".comments").find(".hidden:eq(1)").val();
	var comment_html = $(event.target).parents(".col-sm-8").children(".coment");
	var answer = {"comment" : comment,"heartwordid" : id};
	$.ajax({
		url : "/CLASS/discuss.do",
		data: answer,
		type : "post",
		datatype : "json",
		success : function(data){
			console.log(data);
			if(comment != ""){
				comment_html.append('<div class="col-sm-12 smallComment"><img src="'+ data.comment.photo +'" alt="" class="comment"><div class="col-sm-2"><h6>'+ data.comment.membername +'</h6><p class="font">'+ data.comment.createtime +'</p></div><div class="col-sm-8"><p>'+ data.comment.comment +'</p></div><input type="hidden" class="hidden" value="'+ data.comment.commentid +'"><input type="hidden" class="hidden" value="'+ data.comment.userid +'"><a href="javascript:void(0)" class="icon-trash smalltrash"></a></div>');
				comment_input.val("");
			}else{
				return false;
			}
		}
	})
}
//删除说说功能
function delWord(){
	var heartwordid = $(event.target).parents(".comments").find(".hidden:eq(1)").val();
	var userid = $(event.target).parents(".comments").find(".hidden:eq(2)").val();
	var this_say = $(event.target).parents(".comments");
	$.ajax({
		url : "/CLASS/delword.do?heartwordid="+heartwordid+"&userid="+userid,
		type : "post",
		datatype : "json",
		success : function(data){
			console.log(data);
			if(data.success != false){
				this_say.remove();
				alert(data.message);
			}else{
				alert(data.message);
				return false;
			}
		}
	})
}
//删除评论的功能
function delComment(){
	var commentid = $(event.target).parents(".smallComment").find(".hidden:eq(0)").val();
	console.log(commentid);
	var userid = $(event.target).parents(".smallComment").find(".hidden:eq(1)").val();
	var this_say = $(event.target).parents(".smallComment");
	$.ajax({
		url : "/CLASS/delcomment.do?commentid="+commentid+"&userid="+userid,
		type : "post",
		datatype : "json",
		success : function(data){
			console.log(data);
			if(data.success != false){
				this_say.remove();
				alert(data.message);
			}else{
				alert(data.message);
				return false;
			}
		}
	})
}